clear
set more off
cd "[update to local folder]"

*****************
*Loading the data
*****************

/****************
NOTE: Fragile Families requests that all users register on their website in order to 
access their data and that approved researchers do not redistribute the data. See
https://fragilefamilies.princeton.edu/faq#general_7.

Interested researchers can register and download the Fragile Families data extracts here: 
https://opr.princeton.edu/archive/ff/.
****************/

use "ffmombspv3.dta", replace
merge 1:1 idnum using "ffmom1ypv2.dta"



/* IPV related variables:
Baseline: m1b13b, m1b25b, m1b7b , m2d9a2
Year 1: m2d6i, m2d8i, m2e8i, m2d6h, m2d8h, m2e8h, m2d9a3 */ 

*****************
*Preparing the data
*****************

foreach var in  m1b13b  m1b25b  m1b7b m2d6i  m2d8i   m2e8i   m2d6h   m2d8h   m2e8h  m2d9a2  m2d9a3 m2e9a2  m2e9a3{
    gen c_`var' = `var'
    replace c_`var' = . if c_`var' < 0
}
replace m2d9a2 = 2 if m2d9 == 2
replace m2d9a3 = 2 if m2d9 == 2
replace m2e9a2 = 2 if m2e9 == 2
replace m2e9a3 = 2 if m2e9 == 2

egen baseline_q1 = rowmin(c_m1b13b c_m1b25b c_m1b7b)
egen year1_q1 = rowmin(c_m2d6h   c_m2d8h   c_m2e8h)
egen year1_q2 = rowmin(c_m2d6i  c_m2d8i   c_m2e8i)
egen year1_q3 = rowmin(c_m2d9a2  c_m2e9a2   )
egen year1_q4 = rowmin(c_m2d9a3  c_m2e9a3   )

gen ipv_preg = baseline_q1 == 1 | baseline_q1 == 2 | c_m2d9a2 == 1 | c_m2e9a2 == 1 if (baseline_q1 != . | c_m2d9a2 != . | c_m2e9a2 != .)
gen ipv_post = year1_q2 == 1 | year1_q2 == 2 | year1_q1 == 1 | year1_q1 == 2 | c_m2d9a3 == 1 | c_m2e9a3 == 1 if (year1_q1 != . | year1_q2 != . | c_m2d9a3 != . | c_m2e9a3 != .)

*BASELINE CHARACTERISTICS 
gen race_ethn_hisp  = m1h3a == 1 if m1h3a > 0
gen race_ethn_white = m1h3a != 1 & m1h3 == 1 if m1h3a > 0 & m1h3 > 0
gen race_ethn_black = m1h3a != 1 & m1h3 == 2 if m1h3a > 0 & m1h3 > 0
gen age = cm1age if cm1age > 0
gen educ_lt_hs   = cm1edu == 1 if cm1edu > 0  
gen educ_hs      = cm1edu == 2 if cm1edu > 0  
gen educ_postsec = cm1edu >= 3 if cm1edu > 0  
gen rel_mar = cm1marf == 1 if cm1marf >= 0
gen rel_coh = cm1cohf == 1 if cm1cohf >= 0
foreach var of varlist m1b1a m1b1b{
    gen c_`var' = `var' if `var' >= 0
}
gen rel_dur_prepreg = c_m1b1a + m1b1b/12
gen hh_minors = cm1kids if cm1kids >= 0
gen hh_adults = cm1adult if cm1adult >= 0
gen hh_inc0 = cm1hhinc if cm1hhinc >= 0 
gen hh_pov0 = cm1inpov if cm1inpov >= 0 

*BIRTH CHARACTERISTICS  
gen b_appear_mother = m1a16 == 1 if m1a16 > 0
gen b_appear_father = m1a16 == 2 if m1a16 > 0
gen b_appear_other  = m1a16 >= 3 if m1a16 > 0
gen b_male  = cm1bsex == 1  if cm1bsex > 0
gen hh_inc1 = cm2hhinc if cm2hhinc >= 0 
gen hh_pov1 = cm2povco if cm2povco >= 0 
gen hh_inc_change = hh_inc1 - hh_inc0
gen hh_pov_change = hh_pov1 - hh_pov0


*****************
*COMPARING INITIATION DURING BASELINE VERSUS INITIATION IN YEAR 1 FOLLOW-UP
*****************

*APPENDIX TABLE D.1: BASELINE CHARACTERISTICS 
local label_race_ethn_hisp  "Race/Ethn. = Hispanic"
local label_race_ethn_white "Race/Ethn. = White, Not Hisp."
local label_race_ethn_black "Race/Ethn. = Black, Not Hisp."
local label_age             "Age at Birth"
local label_educ_lt_hs      "Educ. = Less than High School"
local label_educ_hs         "Educ. = High School"
local label_educ_postsec    "Educ. = Post-Secondary Education"
local label_rel_mar         "Married"
local label_rel_coh         "Cohabiting, Not Married"
local label_rel_dur_prepreg "Years knowing father before pregnancy"
local label_hh_minors       "Total minors in Household"
local label_hh_adults       "Total adults in Household"
local label_hh_inc0         "Total Household Income at Baseline"


foreach var in race_ethn_hisp race_ethn_white race_ethn_black age educ_lt_hs educ_hs educ_postsec rel_mar rel_coh rel_dur_prepreg hh_minors hh_adults hh_inc0{
    ttest `var' if (ipv_preg == 1 | ipv_post == 1 )  & ipv_post != . , by(ipv_preg)    
}

*APPENDIX TABLE D.1: BIRTH CHARACTERISTICS  
local label_b_appear_mother "Baby resembles Mother"
local label_b_appear_father "Baby resembles Father"
local label_b_appear_other  "Baby resembles non-parent"
local label_b_male          "Baby is male"
local label_hh_inc_change  "$\Delta$ Household Income"
local label_hh_pov_change  "$\Delta$ Household Poverty Ratio"

foreach var in b_appear_mother b_appear_father b_appear_other b_male hh_inc_change hh_pov_change{
    ttest `var' if (ipv_preg == 1 | ipv_post == 1 )  & ipv_post != . , by(ipv_preg)
}




